package cit.project.capeclear;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class WebActivity extends Activity {

	String webName;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.weblayout);
	
	    // Fills the bundle with the received 
        Bundle b = getIntent().getExtras();
        webName = b.getString("webName");
        String url = b.getString("url");
        
        ((TextView)findViewById(R.id.webTitle)).setText(webName);
	    
	    WebView mWebView = (WebView) findViewById(R.id.webbrowser);
	    mWebView.getSettings().setJavaScriptEnabled(true);
	    mWebView.loadUrl(url);
	    
	    mWebView.setWebViewClient(new capeWebClient());
	}
	

	class capeWebClient extends WebViewClient 
	{
	    @Override
	    public boolean shouldOverrideUrlLoading(WebView view, String url) 
	    {
	    	
	    	  if(webName.equals(getString(R.string.audio)))
	    	  {
	    		  
	    		  Uri myUri = Uri.parse(url);
	    		  
	    		  Log.d("webView", "Audio: " + myUri);
	    		  
	    		  Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
	    		  intent.setDataAndType(myUri, "audio/*"); 
	    		  WebActivity.this.startActivity(intent);
	    		  
	    		  return false;
	    		  //Intent ytIntent = new Intent(Intent., Uri.parse(url));
	    		  // WebActivity.this.startActivity(ytIntent);
	    	  }
	    	  else
	    	  {
	    		  Log.d("webView", "URL: " + url);
	    		  view.loadUrl(url);
	    		  return true;
	    	  }
	    		  
	        
	    }
	    
	    @Override
	    public void onPageFinished(WebView view, String url) 
	    {
	    	super.onPageFinished(view, url);
	    	findViewById(R.id.webProgress).setVisibility(View.INVISIBLE);
	    }
	};
}